package com.noble.glamour.ui.agora.act.video;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.noble.glamour.R;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.util.UIShow;
import com.noble.glamour.ui.agora.RtcUIShow;
import com.noble.glamour.ui.agora.util.RtcJointChannel;
import com.noble.glamour.ui.utils.NoDoubleClickListener;
import com.noble.library.log.PToast;
import com.noble.library.view.base.BasePanel;
import com.noble.library.view.CustomFrameLayout;

/**
 * 视频聊天底部浮层布局
 * Created by Su on 2017/5/31.
 */
public class RtcVideoFooterPanel extends BasePanel {
    private CustomFrameLayout inputLayout;
    private ImageView chat_silence;
    private RtcChatInputPanel inputPanel;
    private ImageView chat_input;

    private boolean isChat = true;   // 是否可聊天
    private boolean isRotary = true; // 是否可大转盘

    public RtcVideoFooterPanel(Context context) {
        super(context);
        setContentView(R.layout.f1_chat_video_footer_panel);
        initView();
    }

    private void initView() {
        chat_silence = (ImageView) findViewById(R.id.chat_silence);
        inputLayout = (CustomFrameLayout) findViewById(R.id.input_layout);
        inputLayout.show(R.id.chat_bottom);
        chat_silence.setSelected(!RtcJointChannel.getInstance().getSwitchAudio());

        chat_input = (ImageView) findViewById(R.id.chat_input);
        if (ModuleMgr.getRtcEnginMgr().isCanChat()) { // 能发送
            chat_input.setVisibility(View.VISIBLE);
            chat_input.setOnClickListener(listener);
        }

        LinearLayout container = (LinearLayout) findViewById(R.id.chat_input_panel);
        inputPanel = new RtcChatInputPanel(getContext());
        container.addView(inputPanel.getContentView());

        chat_silence.setOnClickListener(listener);
        findViewById(R.id.chat_turntable).setOnClickListener(listener);
        findViewById(R.id.chat_gift).setOnClickListener(listener);
        findViewById(R.id.chat_facial).setOnClickListener(listener);
        findViewById(R.id.chat_prop).setOnClickListener(listener);

        inputPanel.setInputPanelListener(new RtcChatInputPanel.InputPanelListener() {
            @Override
            public void onInputbtnClick(View v) {
                inputLayout.show(R.id.chat_bottom);
            }
        });
    }

    /**
     * 更新弹框布局
     */
    public void refreshLayout() {
        inputLayout.show(R.id.chat_bottom);
        inputPanel.refresh(View.GONE);
    }

    /**
     * 聊天开关
     */
    public void refreshChatBtn(boolean isCanChat) {
        chat_input.setVisibility(isCanChat ? View.VISIBLE : View.GONE);
        chat_input.setOnClickListener(isCanChat ? listener : null);
    }

    private void switchLocalAudio() {
        RtcJointChannel.getInstance().switchAudio();
        chat_silence.setSelected(!RtcJointChannel.getInstance().getSwitchAudio());
    }

    /**
     * 聊天开关
     */
    public boolean refreshChatBtn() {
        boolean isCanChat = ModuleMgr.getRtcEnginMgr().isCanChat();
        if (!isCanChat) {
            PToast.showShort("您的发送机会已用完，无法再次发送文字消息");
        }
        chat_input.setVisibility(isCanChat ? View.VISIBLE : View.GONE);
        chat_input.setOnClickListener(isCanChat ? listener : null);
        return isCanChat;
    }

    private NoDoubleClickListener listener = new NoDoubleClickListener() {
        @Override
        public void onNoDoubleClick(View v) {
            switch (v.getId()) {
                case R.id.chat_input:// 发送文字
                    if (!refreshChatBtn() || !isChat) return;
                    inputLayout.show(R.id.chat_input_panel);
                    inputPanel.refresh(View.VISIBLE);
                    break;

                case R.id.chat_silence:// 静音
                    switchLocalAudio();
                    break;

                case R.id.chat_turntable:// 大转盘
                    if (!isRotary || footerListener == null) return;
                    footerListener.onFooterbtnClick(v);
                    break;

                case R.id.chat_gift:// 送礼
                    RtcUIShow.showRtcBottomGiftDlg(getContext(), ModuleMgr.getRtcEnginMgr().getEngineConfig().mOtherId);
                    break;

                case R.id.chat_facial:  // 美颜
                    ModuleMgr.getRtcEnginMgr().switchBeauty();
                    break;

                case R.id.chat_prop:    // 道具
                    RtcUIShow.showRtcFaceUnityDialog((FragmentActivity) getContext());
                    break;
            }
        }
    };

    public void chatEnable(boolean enable) {
        this.isChat = enable;
    }

    private FooterPanelListener footerListener;

    public void setFooterPanelListener(FooterPanelListener footerListener) {
        this.footerListener = footerListener;
    }

    public interface FooterPanelListener {
        void onFooterbtnClick(View v);
    }

    public void refreshRotary(boolean value) {
        this.isRotary = value;
    }
}
